/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.client;

import com.jfinal.core.Controller;
import com.lambkit.Lambkit;
import com.lambkit.common.util.StringUtils;
import com.lambkit.module.upms.UpmsConfig;
import com.lambkit.module.upms.common.LoginService;
import com.lambkit.module.upms.common.UpmsResult;
import com.lambkit.module.upms.common.UpmsResultConstant;
import com.lambkit.plugin.auth.AuthManager;
import com.lambkit.plugin.gateway.GatewayRender;
import com.lambkit.web.WebConfig;
import com.lambkit.web.WebManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class UpmsLoginService implements LoginService {

	public void captcha(Controller controller) {
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("captcha", config.getSsoServerUrl() + "/captcha"));
	}

	public void sms(Controller controller) {
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("sms", config.getSsoServerUrl() + "/sms"));
	}

    public void login(Controller controller) {
    	String username = controller.getPara("username");
		String password = controller.getPara("password");

        if(SsoAuthenticate.validate(controller)) {
        	// 已登陆
			String backurl = controller.getRequest().getParameter("p");
            if (StringUtils.isBlank(backurl)) {
                backurl = "/";
                WebConfig webManage = WebManager.me().getWebConfig("manage");
                WebConfig webAdmin = WebManager.me().getWebConfig("admin");
                Subject subject = SecurityUtils.getSubject();
                backurl = subject.hasRole("admin") ? webManage.getUrl() : "";
                backurl = subject.hasRole("super") ? webAdmin.getUrl() : "";
            }
            controller.redirect(backurl);
            return;
        }

		if (!StringUtils.areNotEmpty(username, password)) {
			controller.createToken("loginToken");
			controller.renderTemplate("login.html");
			return;
		}

		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("login", config.getSsoServerUrl() + "/login"));
	}

	public void logout(Controller controller) {
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		AuthManager.me().getService().logout(controller.getRequest());
		controller.render(new GatewayRender("logout", config.getSsoServerUrl() + "/logout"));
	}

    public void ajaxLogout(Controller controller) {
    	AuthManager.me().getService().logout(controller.getRequest());
    	controller.renderJson(new UpmsResult(UpmsResultConstant.SUCCESS, "logout"));
    }

	@Override
	public void unPasswordLogin(Controller controller) {
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("unpasswordLogin", config.getSsoServerUrl() + "/unpasswordLogin"));
	}

	@Override
	public void authenticate(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("authenticate", config.getSsoServerUrl() + "/authenticate"));
	}

	@Override
	public void userinfo(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("userinfo", config.getSsoServerUrl() + "/userinfo"));
	}

	@Override
	public void regist(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("regist", config.getSsoServerUrl() + "/regist"));
	}

	@Override
	public void repswd(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("repswd", config.getSsoServerUrl() + "/repswd"));
	}

	@Override
	public void forgot(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("forgot", config.getSsoServerUrl() + "/forgot"));
	}

	@Override
	public void reset(Controller controller) {
		// TODO Auto-generated method stub
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		controller.render(new GatewayRender("reset", config.getSsoServerUrl() + "/reset"));
	}
}
